import QtQuick 1.1
import com.nokia.meego 1.0
import TransferUI 1.0

Page {
    id: mainPage

    // A static TransferUI Transfer
    // See TransferUITransferItem class.
    Transfer {
        id: staticTransferItem

        property bool registered: false

        client: transferClient // Declared in main.qml
        iconId: "icon-m-content-file-unknown"
        showInHistory: true

        onCancelled: {
            console.debug("MainPage.qml: Transfer::onCancelled:",
                          "Transfer was cancelled. Removing.");

            if (remove()) {
                console.debug("Transfer removed successfully!");
                registered = false;
            }
            else {
                console.debug("Failed to remove transfer!");
            }
        }
        onPaused: {
            console.debug("MainPage.qml: Transfer::onPaused");
        }
        onStarted: {
            console.debug("MainPage.qml: Transfer::onStarted");
        }
    }

    Flickable {
        id: transferEditorContainer
        anchors.fill: parent
        contentHeight: transferControls.visible ?
                           transferEditor.height + transferControls.height
                         : transferEditor.height;

        // Component for editing the static transfer item
        TransferEditor {
            id: transferEditor
            height: 510

            anchors {
                top: parent.top
                left: parent.left
                right: parent.right
                margins: 30
            }

            name: "filename.png"
            targetName: "Target"
            size: 1024000

            typeButtonsEnabled: !staticTransferItem.registered
            registerButtonEnabled: !staticTransferItem.registered
            commitButtonEnabled: staticTransferItem.registered

            onRegisterButtonClicked: {
                staticTransferItem.name = transferEditor.name;
                staticTransferItem.targetName = transferEditor.targetName;
                staticTransferItem.size = transferEditor.size;

                // Register the transfer and commit the property changes.
                var success =
                    staticTransferItem.registerTransfer(staticTransferItem.name,
                                                        transferEditor.type, "");

                if (success) {
                    console.debug("Transfer registered successfully!");
                    staticTransferItem.registered = true;
                }
                else {
                    console.debug("Failed to register transfer!");
                }
            }

            onCommitButtonClicked: {
                staticTransferItem.name = transferEditor.name;
                staticTransferItem.targetName = transferEditor.targetName;
                staticTransferItem.size = transferEditor.size;

                if (staticTransferItem.commit()) {
                    console.log("Transfer properties committed.");
                }
                else {
                    console.log("Failed to commit transfer properties!");
                }
            }
        } // TransferEditor

        TransferControls {
            id: transferControls
            height: 450

            anchors {
                top: transferEditor.bottom
                left: parent.left
                right: parent.right
                margins: 30
            }

            visible: staticTransferItem.registered
            estimate: 1
            progress: staticTransferItem.progress

            onMarkCancelledButtonClicked: {
                staticTransferItem.markCancelled();
                staticTransferItem.registered = false;
            }
            onMarkCompletedButtonClicked: {
                staticTransferItem.markCompleted();

                if (!staticTransferItem.showInHistory) {
                    staticTransferItem.remove();
                    staticTransferItem.registered = false;
                }
            }
            onMarkDoneButtonClicked: {
                staticTransferItem.markDone();
                staticTransferItem.registered = false;
            }
            onMarkFailureButtonClicked: {
                staticTransferItem.markFailure();

                if (!staticTransferItem.showInHistory) {
                    staticTransferItem.remove();
                    staticTransferItem.registered = false;
                }
            }
            onMarkPausedButtonClicked: {
                staticTransferItem.markPaused();
            }
            onMarkResumedButtonClicked: {
                staticTransferItem.markResumed();
            }
            onSetActiveButtonClicked: {
                staticTransferItem.setActive();
            }
            onSetPendingButtonClicked: {
                staticTransferItem.setPending();
            }
            onEstimateChanged: {
                staticTransferItem.estimate = transferControls.estimate;
                staticTransferItem.commit();
            }
            onProgressChanged: {
                staticTransferItem.progress = transferControls.progress;
                staticTransferItem.commit();
            }
        } // TransferControls
    } // Flickable

    ScrollDecorator {
        flickableItem: transferEditorContainer
    }

    tools: ToolBarLayout {
        ToolIcon { } // Placeholder for the spot of the back button

        // Uncomment the following the add an initialization button. The button
        // should not be needed since the client is initialized during
        // construction.
        /*ToolButton {
            text: "Init"
            onClicked: transferClient.init();
        }*/

        ToolButton {
            text: "Launch UI"
            onClicked: transferClient.showUI();
        }

        // Could be used to add transfer dynamically
        /*ToolIcon {
            iconId: "toolbar-add"
        }*/

        ToolIcon {
            iconId: "toolbar-delete"

            onClicked: {
                if (staticTransferItem.remove()) {
                    console.debug("Transfer removed successfully!");
                    staticTransferItem.registered = false;
                }
                else {
                    console.debug("Failed to remove transfer!");
                }
            }
        }
    }
}

// End of file.
